home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /* swap_control.c - demonstrate swap_control extension
- *
- * Up Arrow - increment swap interval
- * Down Arrow - decrement swap interval
- * Escape key - exit program
- */
- /* compile: cc -o swap_control swap_control.c -lGLU -lGL -lX11 */
-
- #include <GL/glx.h>
- #include <GL/glu.h>
- #include <X11/keysym.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid incInterval( GLvoid );
- GLvoid decInterval( GLvoid );
-
- static void printHelp( char * );
- static void renderBitmapString( GLuint, char * );
- static GLuint LoadFont(Display *dpy, char *fontName);
-
- static char *fontName = "-*-helvetica-*-r-normal-*-14-*-*-*-*-*-*-*";
- static GLuint font;
-
- static int interval = 1;
- static GLfloat angle = 0.0;
-
- static Bool
- process_input( Display *dpy )
- {
- XEvent event;
- static GLboolean animate = GL_TRUE;
- Bool redraw = 0;
- GLsizei w, h;
-
- while (XPending(dpy)) {
- char buf[31];
- KeySym keysym;
-
- XNextEvent(dpy, &event);
- switch(event.type) {
- case Expose:
- redraw = 1;
- break;
- case ConfigureNotify:
- w = event.xconfigure.width;
- h = event.xconfigure.height;
- glViewport(0, 0, w, h);
- redraw = 1;
- break;
- case MapNotify:
- animate = GL_TRUE;
- break;
- case UnmapNotify:
- animate = GL_FALSE;
- break;
- case KeyPress:
- (void) XLookupString(&event.xkey, buf, sizeof(buf),
- &keysym, NULL);
- switch (keysym) {
- case XK_Up: /* increment */
- incInterval();
- break;
- case XK_Down:
- decInterval();
- break;
- case XK_Escape:
- exit(EXIT_SUCCESS);
- break;
- default:
- break;
- }
- default:
- break;
- }
- }
- return redraw || animate;
- }
-
- int
- main(int argc, char **argv)
- {
- int attributeList[] = { GLX_DOUBLEBUFFER,
- GLX_RGBA, GLX_RED_SIZE, 1, None };
- Display *dpy;
- XVisualInfo *vi;
- GLXContext cx;
- Bool isdirect;
- XSetWindowAttributes swa;
- Window win;
- const char *s = NULL;
- int major, minor;
-
-
- dpy = XOpenDisplay(0);
-
- if (!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList))) {
- fprintf(stderr, "overlay: no suitable RGB visual available\n");
- exit(EXIT_FAILURE);
- }
-
- /* create a GLX context */
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- isdirect = glXIsDirect(dpy, cx);
-
- swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
- vi->visual, AllocNone);
-
- swa.border_pixel = 0;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
- win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 500, 500,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XStoreName(dpy, win, argv[0]);
- XMapWindow(dpy, win);
-
- glXMakeCurrent(dpy, win, cx);
-
- font = LoadFont( dpy, fontName );
-
- #ifdef GLX_SGI_swap_control
- if ( glXQueryVersion(dpy, &major, &minor) == GL_FALSE ) {
- fprintf(stderr, "glXQueryVersion() failed.\n");
- exit(-1);
- }
- #ifdef GLX_VERSION_1_1
- if ( minor > 0 || major > 1 )
- s = glXQueryExtensionsString( dpy, DefaultScreen(dpy) );
- #endif
- #endif
- if (!s || !strstr(s, "GLX_SGI_swap_control")) {
- fprintf(stderr, "GLX_SGI_swap_control not supported\n");
- }
-
- initgfx();
-
- printHelp( argv[0] );
-
- while (1) {
- if (process_input(dpy)) {
- drawScene();
- glXSwapBuffers(dpy, win);
- if (!isdirect) glFinish();
- }
- }
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates swap_control extension\n"
- "Escape key - exit the program\n"
- "Up Arrow - increment swap interval\n"
- "Down Arrow - decrement swap interval\n\n",
- progname);
- }
-
- static GLuint
- LoadFont(Display *dpy, char *fontName)
- {
- XFontStruct *fontInfo;
- unsigned long first, last;
- GLuint fontBase;
-
- fontInfo = XLoadQueryFont(dpy, fontName);
- if (fontInfo == NULL) {
- fprintf(stderr, "Can't find font \"%s\"\n", fontName);
- return;
- }
-
- first = fontInfo->min_char_or_byte2;
- last = fontInfo->max_char_or_byte2;
-
- fontBase = glGenLists(last+1);
- if (fontBase == 0) {
- fprintf(stderr, "Out of list space\n");
- return;
- }
- glXUseXFont(fontInfo->fid, first, last-first+1, fontBase+first);
-
- return( fontBase );
- }
-
- GLvoid
- renderBitmapString( GLuint fontBase, char *string )
- {
- glPushAttrib( GL_LIST_BIT );
- glListBase( fontBase );
- glCallLists( strlen(string), GL_UNSIGNED_BYTE, string );
- glPopAttrib();
- }
-
-
- GLvoid
- initgfx( GLvoid )
- {
- glClearColor( 0.5, 0.5, 0.5, 1.0 );
-
- #ifdef GLX_SGI_swap_control
- glXSwapIntervalSGI( interval );
- #endif
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
-
- GLvoid
- incInterval( GLvoid )
- {
- interval++;
- #ifdef GLX_SGI_swap_control
- glXSwapIntervalSGI( interval );
- #endif
- }
-
- GLvoid
- decInterval( GLvoid )
- {
- if ( interval > 1 ) {
- interval--;
- #ifdef GLX_SGI_swap_control
- glXSwapIntervalSGI( interval );
- #endif
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- char buf[20];
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glPushMatrix();
- glRotatef( angle, 0.0, 0.0, -1.0 );
- glColor3f( 1.0, 1.0, 0.0 );
- glBegin( GL_TRIANGLES );
- glVertex2f( -0.1, -0.1 );
- glVertex2f( 0.1, -0.1 );
- glVertex2f( 0.0, 0.9 );
- glEnd();
- glPopMatrix();
-
- /* print stat info */
- glColor3f( 0.0, 0.0, 0.0 );
- sprintf( buf, "swap interval: %3d", interval );
- glRasterPos2f( -0.5, -0.5 );
- renderBitmapString( font, buf );
-
- angle = fmodf( (angle + 6.0), 360.0 );
- }
-